﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyAlbum.Services;
#if StructureMap
//using StructureMap;
//using StructureMap.Configuration.DSL;

//using StructureMap.Attributes;
//using StructureMap.Configuration;
#endif
using MyAlbum.Data;
using MyAlbum.Logging;
using MyAlbum.Model;
using PersonalAlbum.Controllers;
using System.Web.Security;
using PersonalAlbum.Caching;

namespace PersonalAlbum
{
   
    public static class Bootstrapper
    {
        public static void ConfigureAutoWiring()
        {
           


              //   ForRequestedType<ICacheService>().TheDefaultIsConcreteType<WebCacheManager>().CacheBy(InstanceScope.Singleton);
            //        ForRequestedType<IFormsAuthentication>().
            //            TheDefaultIsConcreteType<FormsAuthenticationWrapper>();

            //        ForRequestedType<MembershipProvider>().TheDefault.Is.ConstructedBy(() => Membership.Provider);

            //        ForRequestedType<ILogger>().
            //            TheDefaultIsConcreteType<NLogger>().
            //            CacheBy(InstanceScope.Hybrid);

            //        ForRequestedType<ILanguageService>().
            //            TheDefaultIsConcreteType<Lang>().
            //            CacheBy(InstanceScope.Hybrid);
        }
        #if StructureMap
        public static void ConfigureStructureMap()
        {
             StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
             StructureMapConfiguration.AddRegistry(new PersonalAlbumRegistry());
        }
#endif
        //public static T GetInstance<T>()
        //{
        //    return ObjectFactory.GetInstance<T>();
        //}
    }
    #if StructureMap
    public class PersonalAlbumRegistry : Registry
    {
        protected override void configure()
        {
            RegisterServices();
            ForRequestedType<ICacheService>().TheDefaultIsConcreteType<WebCacheManager>().CacheBy(InstanceScope.Singleton);
            ForRequestedType<IFormsAuthentication>().
                TheDefaultIsConcreteType<FormsAuthenticationWrapper>();

            ForRequestedType<MembershipProvider>().TheDefault.Is.ConstructedBy(() => Membership.Provider);

            ForRequestedType<ILogger>().
                TheDefaultIsConcreteType<NLogger>().
                CacheBy(InstanceScope.Hybrid);

            ForRequestedType<ILanguageService>().
                TheDefaultIsConcreteType<Lang>().
                CacheBy(InstanceScope.Hybrid);

            RegisterLinqDataProvider();


        }

        private void RegisterServices()
        {
            ForRequestedType<ITagEntryService>()
             .TheDefaultIsConcreteType<TagEntryService>()
             .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<IEntryService>()
             .TheDefaultIsConcreteType<EntryService>()
             .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<ISectionService>()
                .TheDefaultIsConcreteType<SectionService>()
                .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<IDataService<Album>>().
               TheDefaultIsConcreteType<AlbumService>()
               .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<IDataService<Config>>().
               TheDefaultIsConcreteType<ConfigService>()
               .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<IDataService<Support>>().
              TheDefaultIsConcreteType<SupportService>()
              .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<IDataService<Reply>>().
               TheDefaultIsConcreteType<ReplyService>()
               .CacheBy(InstanceScope.Hybrid);

            ForRequestedType<IDataService<Entrance>>().
              TheDefaultIsConcreteType<EntranceService>()
              .CacheBy(InstanceScope.Hybrid);
        }
        private void RegisterLinqDataProvider()
        {
            ForRequestedType<ITagEntryRepository>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.TagEntryRepository(Utils.ConnectionString));

            ForRequestedType<IEntryRepository>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.EntryRepository(Utils.ConnectionString));

            ForRequestedType<IRepository<Section>>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.SectionRepository(Utils.ConnectionString));

            ForRequestedType<IRepository<Album>>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.AlbumRepository(Utils.ConnectionString));

            ForRequestedType<IRepository<Config>>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.ConfigRepository(Utils.ConnectionString));

            ForRequestedType<IRepository<Support>>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.SupportRepository(Utils.ConnectionString));

            ForRequestedType<IRepository<Reply>>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.ReplyRepository(Utils.ConnectionString));

            ForRequestedType<IRepository<Entrance>>().TheDefault.Is.ConstructedBy(() => new MyAlbum.Data.Linq.EntranceRepository(Utils.ConnectionString));


        }
    }
#endif
}
